{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "# os.environ[\"GYPB_ENV\"] = \"production\"\n",
    "import empyrical\n",
    "import pandas as pd\n",
    "from xq_factor import Config\n",
    "from xq_factor.factor import LeafFactor, ObjectedLeafFactor, AbstractFactor\n",
    "from xq_factor import set_option, add_api, get_option\n",
    "from xqdata.constant import Frequency\n",
    "from gypb.db import get_ddbapi\n",
    "from xq_factor.operator import PROPORTION, RANK, FFILL, DIFF, REF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import rqdatac"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\09_GYPB\\.venv\\lib\\site-packages\\rqdatac\\client.py:257: UserWarning: Your account will be expired after  205 days. Please call us at 0755-22676337 to upgrade or purchase or renew your contract.\n",
      "  warnings.warn(\"Your account will be expired after  {} days. \"\n"
     ]
    }
   ],
   "source": [
    "rqdatac.init(\"license\",\"DWAiwIzXRSu6LZ3rHvp5KwYn40JtXD_rZ01jxu1w69pEyr8O3oOUXpxxFIwuqTa4_dGp1Y3DIaTzYJgJzdhrVVWgLQmdNOZ0YvwdCeNa0wvwWfPyayGgXOZiEr9bIKZcK8budV1mSja5dJ5hMnmJRr1AUaizvYnJ-V0uwo7j9ko=XPIhgMC-376Cm7rxQaejCLp-tw5GpQK5J2WXM2MeJtXefVB0zMcdIotTMOYNVc4_M_Rs_t7qiS52mVyttafbkEyM2qDdRcOBoq5-LDicbhH1-vNWOPN5pC-uRUN8IQ_zIHgPGr6-DikHJc9gJuyRznCrK2f5ZW9ZJ2stnvSyJfI=\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "ddbapi = get_ddbapi()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "ddbnv_df = ddbapi.get_factor([\"netvalue\",\"cum_netvalue\"],[\"SSM234\",\"SQN990\"],\"2024-01-01\",\"2024-03-31\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "ddbnv_df = ddbnv_df.reset_index().rename(columns={\"datetime\":\"date\",\"netvalue\":\"price\",\"cum_netvalue\":\"cumulative_nav_withdrawal\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "ddbnv_df.date = ddbnv_df.date.astype(str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "def groupby_to_list_of_dict(df):\n",
    "    df = df.reset_index().rename(\n",
    "        columns={\n",
    "            \"datetime\": \"date\",\n",
    "            \"netvalue\": \"price\",\n",
    "            \"cum_netvalue\": \"cumulative_nav_withdrawal\",\n",
    "        }\n",
    "    )\n",
    "    for col in [\"date\",\"price\",\"cumulative_nav_withdrawal\"]:\n",
    "        df[col] = df[col].astype(str)\n",
    "    # 初始化结果列表\n",
    "    result = []\n",
    "\n",
    "    # 按指定列进行分组\n",
    "    for group_key, group_data in df.groupby(\"code\"):\n",
    "        # 为每个分组创建一个字典\n",
    "        group_dict = {\n",
    "            \"code\": group_key,  # 分组值作为第一个key的值\n",
    "            \"nets\": [],  # 初始化数据列表\n",
    "        }\n",
    "\n",
    "        # 遍历分组内的每一行，转换为字典并添加到data列表中\n",
    "        for _, row in group_data.iterrows():\n",
    "            # 排除分组列，将剩余列转换为字典\n",
    "            row_dict = row.drop(\"code\").to_dict()\n",
    "            group_dict[\"nets\"].append(row_dict)\n",
    "\n",
    "        # 将分组字典添加到结果列表\n",
    "        result.append(group_dict)\n",
    "\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'code': 'SQN990',\n",
       "  'nets': [{'date': Timestamp('2024-01-02 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.0582,\n",
       "    'price': 1.0582},\n",
       "   {'date': Timestamp('2024-01-03 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.0575,\n",
       "    'price': 1.0575},\n",
       "   {'date': Timestamp('2024-01-04 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.0517,\n",
       "    'price': 1.0517},\n",
       "   {'date': Timestamp('2024-01-05 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.0377,\n",
       "    'price': 1.0377},\n",
       "   {'date': Timestamp('2024-01-08 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.0244,\n",
       "    'price': 1.0244},\n",
       "   {'date': Timestamp('2024-01-09 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.027,\n",
       "    'price': 1.027},\n",
       "   {'date': Timestamp('2024-01-10 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.0209,\n",
       "    'price': 1.0209},\n",
       "   {'date': Timestamp('2024-01-11 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.0265,\n",
       "    'price': 1.0265},\n",
       "   {'date': Timestamp('2024-01-12 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.0228,\n",
       "    'price': 1.0228},\n",
       "   {'date': Timestamp('2024-01-15 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.0201,\n",
       "    'price': 1.0201},\n",
       "   {'date': Timestamp('2024-01-16 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.0165,\n",
       "    'price': 1.0165},\n",
       "   {'date': Timestamp('2024-01-17 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.0011,\n",
       "    'price': 1.0011},\n",
       "   {'date': Timestamp('2024-01-18 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9947,\n",
       "    'price': 0.9947},\n",
       "   {'date': Timestamp('2024-01-19 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9844,\n",
       "    'price': 0.9844},\n",
       "   {'date': Timestamp('2024-01-22 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9418,\n",
       "    'price': 0.9418},\n",
       "   {'date': Timestamp('2024-01-23 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.94,\n",
       "    'price': 0.94},\n",
       "   {'date': Timestamp('2024-01-24 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9439,\n",
       "    'price': 0.9439},\n",
       "   {'date': Timestamp('2024-01-25 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9675,\n",
       "    'price': 0.9675},\n",
       "   {'date': Timestamp('2024-01-26 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9662,\n",
       "    'price': 0.9662},\n",
       "   {'date': Timestamp('2024-01-29 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9522,\n",
       "    'price': 0.9522},\n",
       "   {'date': Timestamp('2024-01-30 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.934,\n",
       "    'price': 0.934},\n",
       "   {'date': Timestamp('2024-01-31 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9129,\n",
       "    'price': 0.9129},\n",
       "   {'date': Timestamp('2024-02-01 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9031,\n",
       "    'price': 0.9031},\n",
       "   {'date': Timestamp('2024-02-02 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8776,\n",
       "    'price': 0.8776},\n",
       "   {'date': Timestamp('2024-02-05 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8463,\n",
       "    'price': 0.8463},\n",
       "   {'date': Timestamp('2024-02-06 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8668,\n",
       "    'price': 0.8668},\n",
       "   {'date': Timestamp('2024-02-07 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.863,\n",
       "    'price': 0.863},\n",
       "   {'date': Timestamp('2024-02-08 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8858,\n",
       "    'price': 0.8858},\n",
       "   {'date': Timestamp('2024-02-19 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8962,\n",
       "    'price': 0.8962},\n",
       "   {'date': Timestamp('2024-02-20 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8978,\n",
       "    'price': 0.8978},\n",
       "   {'date': Timestamp('2024-02-21 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8984,\n",
       "    'price': 0.8984},\n",
       "   {'date': Timestamp('2024-02-22 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9054,\n",
       "    'price': 0.9054},\n",
       "   {'date': Timestamp('2024-02-23 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9109,\n",
       "    'price': 0.9109},\n",
       "   {'date': Timestamp('2024-02-26 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9142,\n",
       "    'price': 0.9142},\n",
       "   {'date': Timestamp('2024-02-27 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9271,\n",
       "    'price': 0.9271},\n",
       "   {'date': Timestamp('2024-02-28 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9,\n",
       "    'price': 0.9},\n",
       "   {'date': Timestamp('2024-02-29 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9223,\n",
       "    'price': 0.9223},\n",
       "   {'date': Timestamp('2024-03-01 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9291,\n",
       "    'price': 0.9291},\n",
       "   {'date': Timestamp('2024-03-04 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.935,\n",
       "    'price': 0.935},\n",
       "   {'date': Timestamp('2024-03-05 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9281,\n",
       "    'price': 0.9281},\n",
       "   {'date': Timestamp('2024-03-06 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9344,\n",
       "    'price': 0.9344},\n",
       "   {'date': Timestamp('2024-03-07 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9296,\n",
       "    'price': 0.9296},\n",
       "   {'date': Timestamp('2024-03-08 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9424,\n",
       "    'price': 0.9424},\n",
       "   {'date': Timestamp('2024-03-11 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9576,\n",
       "    'price': 0.9576},\n",
       "   {'date': Timestamp('2024-03-12 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9631,\n",
       "    'price': 0.9631},\n",
       "   {'date': Timestamp('2024-03-13 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9672,\n",
       "    'price': 0.9672},\n",
       "   {'date': Timestamp('2024-03-14 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9726,\n",
       "    'price': 0.9726},\n",
       "   {'date': Timestamp('2024-03-15 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9889,\n",
       "    'price': 0.9889},\n",
       "   {'date': Timestamp('2024-03-18 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.0068,\n",
       "    'price': 1.0068},\n",
       "   {'date': Timestamp('2024-03-19 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9992,\n",
       "    'price': 0.9992},\n",
       "   {'date': Timestamp('2024-03-20 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.0068,\n",
       "    'price': 1.0068},\n",
       "   {'date': Timestamp('2024-03-21 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.0136,\n",
       "    'price': 1.0136},\n",
       "   {'date': Timestamp('2024-03-22 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9994,\n",
       "    'price': 0.9994},\n",
       "   {'date': Timestamp('2024-03-25 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9804,\n",
       "    'price': 0.9804},\n",
       "   {'date': Timestamp('2024-03-26 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9842,\n",
       "    'price': 0.9842},\n",
       "   {'date': Timestamp('2024-03-27 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9619,\n",
       "    'price': 0.9619},\n",
       "   {'date': Timestamp('2024-03-28 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9863,\n",
       "    'price': 0.9863},\n",
       "   {'date': Timestamp('2024-03-29 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.0018,\n",
       "    'price': 1.0018}]},\n",
       " {'code': 'SSM234',\n",
       "  'nets': [{'date': Timestamp('2024-01-02 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.014,\n",
       "    'price': 1.014},\n",
       "   {'date': Timestamp('2024-01-03 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.0093,\n",
       "    'price': 1.0093},\n",
       "   {'date': Timestamp('2024-01-04 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.0043,\n",
       "    'price': 1.0043},\n",
       "   {'date': Timestamp('2024-01-05 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9853,\n",
       "    'price': 0.9853},\n",
       "   {'date': Timestamp('2024-01-08 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9674,\n",
       "    'price': 0.9674},\n",
       "   {'date': Timestamp('2024-01-09 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9716,\n",
       "    'price': 0.9716},\n",
       "   {'date': Timestamp('2024-01-10 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9612,\n",
       "    'price': 0.9612},\n",
       "   {'date': Timestamp('2024-01-11 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9796,\n",
       "    'price': 0.9796},\n",
       "   {'date': Timestamp('2024-01-12 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9714,\n",
       "    'price': 0.9714},\n",
       "   {'date': Timestamp('2024-01-15 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9701,\n",
       "    'price': 0.9701},\n",
       "   {'date': Timestamp('2024-01-16 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9658,\n",
       "    'price': 0.9658},\n",
       "   {'date': Timestamp('2024-01-17 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.941,\n",
       "    'price': 0.941},\n",
       "   {'date': Timestamp('2024-01-18 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9389,\n",
       "    'price': 0.9389},\n",
       "   {'date': Timestamp('2024-01-19 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9284,\n",
       "    'price': 0.9284},\n",
       "   {'date': Timestamp('2024-01-22 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8767,\n",
       "    'price': 0.8767},\n",
       "   {'date': Timestamp('2024-01-23 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8879,\n",
       "    'price': 0.8879},\n",
       "   {'date': Timestamp('2024-01-24 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8994,\n",
       "    'price': 0.8994},\n",
       "   {'date': Timestamp('2024-01-25 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9293,\n",
       "    'price': 0.9293},\n",
       "   {'date': Timestamp('2024-01-26 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.923,\n",
       "    'price': 0.923},\n",
       "   {'date': Timestamp('2024-01-29 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8965,\n",
       "    'price': 0.8965},\n",
       "   {'date': Timestamp('2024-01-30 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8703,\n",
       "    'price': 0.8703},\n",
       "   {'date': Timestamp('2024-01-31 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8343,\n",
       "    'price': 0.8343},\n",
       "   {'date': Timestamp('2024-02-01 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8256,\n",
       "    'price': 0.8256},\n",
       "   {'date': Timestamp('2024-02-02 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.7945,\n",
       "    'price': 0.7945},\n",
       "   {'date': Timestamp('2024-02-05 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.734,\n",
       "    'price': 0.734},\n",
       "   {'date': Timestamp('2024-02-06 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.765,\n",
       "    'price': 0.765},\n",
       "   {'date': Timestamp('2024-02-07 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.7739,\n",
       "    'price': 0.7739},\n",
       "   {'date': Timestamp('2024-02-08 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8135,\n",
       "    'price': 0.8135},\n",
       "   {'date': Timestamp('2024-02-19 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8344,\n",
       "    'price': 0.8344},\n",
       "   {'date': Timestamp('2024-02-20 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8448,\n",
       "    'price': 0.8448},\n",
       "   {'date': Timestamp('2024-02-21 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8538,\n",
       "    'price': 0.8538},\n",
       "   {'date': Timestamp('2024-02-22 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.874,\n",
       "    'price': 0.874},\n",
       "   {'date': Timestamp('2024-02-23 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8964,\n",
       "    'price': 0.8964},\n",
       "   {'date': Timestamp('2024-02-26 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9046,\n",
       "    'price': 0.9046},\n",
       "   {'date': Timestamp('2024-02-27 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9312,\n",
       "    'price': 0.9312},\n",
       "   {'date': Timestamp('2024-02-28 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.8794,\n",
       "    'price': 0.8794},\n",
       "   {'date': Timestamp('2024-02-29 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9151,\n",
       "    'price': 0.9151},\n",
       "   {'date': Timestamp('2024-03-01 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9289,\n",
       "    'price': 0.9289},\n",
       "   {'date': Timestamp('2024-03-04 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9343,\n",
       "    'price': 0.9343},\n",
       "   {'date': Timestamp('2024-03-05 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9248,\n",
       "    'price': 0.9248},\n",
       "   {'date': Timestamp('2024-03-06 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9295,\n",
       "    'price': 0.9295},\n",
       "   {'date': Timestamp('2024-03-07 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9188,\n",
       "    'price': 0.9188},\n",
       "   {'date': Timestamp('2024-03-08 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9315,\n",
       "    'price': 0.9315},\n",
       "   {'date': Timestamp('2024-03-11 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.95,\n",
       "    'price': 0.95},\n",
       "   {'date': Timestamp('2024-03-12 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9613,\n",
       "    'price': 0.9613},\n",
       "   {'date': Timestamp('2024-03-13 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9666,\n",
       "    'price': 0.9666},\n",
       "   {'date': Timestamp('2024-03-14 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9592,\n",
       "    'price': 0.9592},\n",
       "   {'date': Timestamp('2024-03-15 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9709,\n",
       "    'price': 0.9709},\n",
       "   {'date': Timestamp('2024-03-18 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9885,\n",
       "    'price': 0.9885},\n",
       "   {'date': Timestamp('2024-03-19 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.986,\n",
       "    'price': 0.986},\n",
       "   {'date': Timestamp('2024-03-20 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.0003,\n",
       "    'price': 1.0003},\n",
       "   {'date': Timestamp('2024-03-21 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 1.0038,\n",
       "    'price': 1.0038},\n",
       "   {'date': Timestamp('2024-03-22 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9923,\n",
       "    'price': 0.9923},\n",
       "   {'date': Timestamp('2024-03-25 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9675,\n",
       "    'price': 0.9675},\n",
       "   {'date': Timestamp('2024-03-26 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9642,\n",
       "    'price': 0.9642},\n",
       "   {'date': Timestamp('2024-03-27 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9321,\n",
       "    'price': 0.9321},\n",
       "   {'date': Timestamp('2024-03-28 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9564,\n",
       "    'price': 0.9564},\n",
       "   {'date': Timestamp('2024-03-29 00:00:00'),\n",
       "    'cumulative_nav_withdrawal': 0.9643,\n",
       "    'price': 0.9643}]}]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "groupby_to_list_of_dict(ddbnv_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "add_api(ddbapi)\n",
    "set_option(\"start_time\", \"2020-01-02\")\n",
    "set_option(\"end_time\", \"2025-03-28\")\n",
    "set_option(\"frequency\", Frequency.DAILY)\n",
    "set_option(\"universe\", [\"SSM234\",\"SB8975\"])\n",
    "set_option(\"api\", ddbapi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "netvalue = FFILL(LeafFactor(\"netvalue\")).value\n",
    "cum_netvalue = FFILL(LeafFactor(\"cum_netvalue\")).value\n",
    "returns = cum_netvalue.diff() / netvalue.shift(1)\n",
    "cum_returns = (returns + 1).cumprod()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def resample_index(index, freq):\n",
    "    FREQ_MAP = {\n",
    "        Frequency.WEEKLY: \"W\",\n",
    "        Frequency.MONTHLY: \"ME\",\n",
    "        Frequency.ANNUALLY: \"YE\",\n",
    "    }\n",
    "    freq_str = FREQ_MAP.get(freq, None)\n",
    "    new_index = (\n",
    "        pd.Series(index=index, data=index)\n",
    "        .groupby(pd.Grouper(freq=freq_str))\n",
    "        .last()\n",
    "        .dropna()\n",
    "    )\n",
    "    return pd.DatetimeIndex(new_index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "week_index = resample_index(netvalue.index, Frequency.WEEKLY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "week_cum_returns = cum_returns.reindex(week_index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "week_returns = week_cum_returns.pct_change()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "SSM234    0.076761\n",
       "SB8975    0.002492\n",
       "dtype: float64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "empyrical.annual_return(week_returns, period=\"weekly\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "SSM234    181\n",
       "SB8975     73\n",
       "dtype: int64"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "week_returns.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>code</th>\n",
       "      <th>SSM234</th>\n",
       "      <th>SB8975</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datetime</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-10</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-17</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-23</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02-07</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-02-28</th>\n",
       "      <td>-0.019995</td>\n",
       "      <td>-0.023355</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-03-07</th>\n",
       "      <td>0.045687</td>\n",
       "      <td>0.018901</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-03-14</th>\n",
       "      <td>0.020736</td>\n",
       "      <td>0.027160</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-03-21</th>\n",
       "      <td>-0.008321</td>\n",
       "      <td>0.006386</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-03-28</th>\n",
       "      <td>-0.022148</td>\n",
       "      <td>-0.005057</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>268 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "code          SSM234    SB8975\n",
       "datetime                      \n",
       "2020-01-03       NaN       NaN\n",
       "2020-01-10       NaN       NaN\n",
       "2020-01-17       NaN       NaN\n",
       "2020-01-23       NaN       NaN\n",
       "2020-02-07       NaN       NaN\n",
       "...              ...       ...\n",
       "2025-02-28 -0.019995 -0.023355\n",
       "2025-03-07  0.045687  0.018901\n",
       "2025-03-14  0.020736  0.027160\n",
       "2025-03-21 -0.008321  0.006386\n",
       "2025-03-28 -0.022148 -0.005057\n",
       "\n",
       "[268 rows x 2 columns]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "week_returns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "gypb-py3.10",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
